package com.hotai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hotai.comment.dtos.PageResponseResult;
import com.hotai.comment.dtos.ResponseResult;
import com.hotai.comment.enums.AppHttpCodeEnum;
import com.hotai.comment.pojo.*;
import com.hotai.mapper.CorsewareCourseMapper;
import com.hotai.mapper.CorsewareEquipmentMapper;
import com.hotai.mapper.CourseMapper;
import com.hotai.service.CourseService;
import com.hotai.vo.course.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
public class CourseServiecImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private CorsewareEquipmentMapper corsewareEquipmentMapper;
    @Autowired
    private CorsewareCourseMapper  corsewareCourseMapper;

    @Override
    public ResponseResult PageList(CoursePage query) {
        //参数为空
        if (query==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        query.checkParam();

        if (query.getFname()==null){
            query.setFname("科学实验");
        }
        if (query.getSubject()==null){
            query.setSubject("物理");
        }
        LambdaQueryWrapper<Course> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (query!=null){
            lambdaQueryWrapper
                    .eq(StringUtils.isNotBlank(query.getSubject()), Course::getSubject, query.getSubject())
                    .eq(StringUtils.isNotBlank(query.getFname()),  Course::getFname,  query.getFname())
                    .eq(Course::getDeid,1);
        }
        Page page2 = new Page(query.getPage(),query.getSize());
        IPage page = page(page2, lambdaQueryWrapper);
        ResponseResult responseResult = new PageResponseResult(query.getPage(),query.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return  responseResult;
    }


    @Override
    public ResponseResult deleteCourse(ArrayList<Integer> id) {
        for (Integer eid : id) {
            courseMapper.deleteId(eid);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult Coursdetail(CourseDetail queryware) {
        //参数为空
        if (queryware==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }


        //检查参数
        queryware.checkParam();
        queryware.setPage((queryware.getPage()-1)*queryware.getSize());

            ArrayList<CourseDetailList> detaile = courseMapper.courseDatail(queryware);
            PageResponseResult responseResult = new PageResponseResult(queryware.getPage()+1,queryware.getSize(), detaile.size());
            responseResult.setData(detaile);
            return responseResult;
        }

    @Override
    public ResponseResult deleteWare(Integer cid ,Integer wid) {
        courseMapper.deleteCourse(cid,wid);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult WarePageList(CoursePage query) {
        //参数为空
        if (query==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        query.checkParam();

        if (query.getFname()==null){
            query.setFname("实验");
        }
        if (query.getSubject()==null){
            query.setSubject("物理");
        }
        //检查参数
        query.checkParam();
        query.setPage((query.getPage()-1)*query.getSize());

        ArrayList<CourseWareTear> detaile = courseMapper.courseDatailTea(query);
        //设备
        for (CourseWareTear courseWareTear : detaile) {
            List<CoursewareEquipment> coursewareEquipments = corsewareEquipmentMapper.selectList(Wrappers.<CoursewareEquipment>lambdaQuery().eq(CoursewareEquipment::getCid, courseWareTear.getId()));
            courseWareTear.setShe(coursewareEquipments.size());
        }
        //课件
        for (CourseWareTear courseWareTear : detaile) {
            List<CourseCourseware> coursewareEquipments = corsewareCourseMapper.selectList(Wrappers.<CourseCourseware>lambdaQuery().eq(CourseCourseware::getWid, courseWareTear.getId()));
            courseWareTear.setKe(coursewareEquipments.size());
        }

        PageResponseResult responseResult = new PageResponseResult(query.getPage()+1,query.getSize(), detaile.size());
        responseResult.setData(detaile);
        return responseResult;
    }

    @Override
    public ResponseResult deleteEquipment(Integer cid, Integer eid) {
        corsewareEquipmentMapper.deleteEqu(cid,eid);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult EquPageList(CoursePage query) {
        //参数为空
        if (query==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查参数
        query.checkParam();
        query.setPage((query.getPage()-1)*query.getSize());

        ArrayList<CouserEquList> detaile = courseMapper.courseDatailEqu(query);
        PageResponseResult responseResult = new PageResponseResult(query.getPage()+1,query.getSize(), detaile.size());
        responseResult.setData(detaile);
        return responseResult;
    }

    @Override
    public ResponseResult AddEquiment(CoursewareEquipment coursewareEquipment) {
        corsewareEquipmentMapper.insert(coursewareEquipment);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult AddWareCour(CourseCourseware courseCourseware) {
        corsewareCourseMapper.insert(courseCourseware);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS,AppHttpCodeEnum.SUCCESS.getErrorMessage());
    }

    @Override
    public ResponseResult CoursdetailTear(CourseDetail queryware) {
        //参数为空
        if (queryware==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }


        //检查参数
        queryware.checkParam();
        queryware.setPage((queryware.getPage()-1)*queryware.getSize());

        ArrayList<CourseDetailList> detaile = courseMapper.courseTearDatail(queryware);
        PageResponseResult responseResult = new PageResponseResult(queryware.getPage()+1,queryware.getSize(), detaile.size());
        responseResult.setData(detaile);
        return responseResult;
    }

}
